package com.foolishbird.sdk.ui.view;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.foolishbird.sdk.utils.ResUtils;
import com.foolishbird.sdk.utils.WL;


public class ConfirmDialog {

    final Builder builder;
    private final Dialog dialog;

    private ConfirmDialog(final Builder builder) {
        this.builder = builder;
        this.dialog = builder.dialog;
    }

    public void show() {
        if (dialog != null&& !dialog.isShowing()) {
            dialog.show();
        }
    }

    public void dismiss() {
        if (dialog != null&& dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    public void setOnClickListener(View.OnClickListener listener){
        builder.setOnClickListener(listener);
    }

    public void setCancelable(boolean flag){
        if(dialog != null){
            dialog.setCancelable(flag);
        }
    }

    static class Builder implements View.OnClickListener {

        private Context context;
        private View contentView;
        private TextView message;
        private Button confirm;
        private Dialog dialog;


        public Builder(Context context) {
            this.context = context;
            final int layoutResId = ResUtils.getLayoutIdentifier(context, "wan_dialog_confirm");
            final int msgResId = ResUtils.getViewIdentifier(context, "dialog_confirm_message_tv");
            final int confirmResId = ResUtils.getViewIdentifier(context, "dialog_confirm_btn");
            contentView = LayoutInflater.from(context).inflate(layoutResId, null);
            message = (TextView) contentView.findViewById(msgResId);
            confirm = (Button) contentView.findViewById(confirmResId);
            confirm.setOnClickListener(this);
            WL.e("ConfirmDialog","ConfirmDialog");
        }

        public Builder setMessage(String message) {
            if (TextUtils.isEmpty(message)) {
                this.message.setText("");
            } else {
                this.message.setText(message);
            }
            return this;
        }

        public Builder setConfirmText(String text) {
            if (!TextUtils.isEmpty(text)) {
                this.confirm.setText(text);
            }
            return this;
        }

        public Builder setOnClickListener(View.OnClickListener listener) {
            this.confirm.setOnClickListener(listener);
            return this;
        }

        public ConfirmDialog create() {
            dialog = new Dialog(context, ResUtils.getStyleIdentifier(context, "hnssn_dialog"));
            dialog.setContentView(contentView);
            dialog.setCanceledOnTouchOutside(false);
            return new ConfirmDialog(this);
        }

        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    }


}
